﻿@model TestOnlineProject.Models.Entities.Test
@{
    ViewBag.Title = "TestResults";
}
<script>
    $(document).ready(function () {
        // Initial page.
        goToPageResult(1, @ViewBag.totalPageResult);
    });

    // Goto Page.
    function goToPageResult(page, totalPage)
    {
        $(".userTest").hide();
        $(".ut" + page).show();
    }
</script>
<div class="site_map">
    @Html.ActionLink("Home", "Index", "Home") / @Html.ActionLink("Subjects", "Index", "Subject", new { }, null)
    / <span style="word-wrap: break-word">@Html.ActionLink(Model.Subject.Code, "Details", "Subject", new { id = Model.SubjectId }, null)</span>
    /
    @Html.ActionLink("Tests", "Index", "Test", new { subjectId = Model.SubjectId }, null)
    / <span style="word-wrap: break-word">
        @Html.ActionLink(Model.Title, "Details", "Test", new { id = Model.TestId }, null)
    </span>/ Test Results
</div>
<br />
<div class="main_nav">
    <span><a href="@Url.Content("~/Test/Details")?id=@Model.TestId">
        <img src="@Url.Content("~/Images/back button.png")" style="height:30px"/></a>
</div>
<br />
<div id="result_list">
    @{
        if (Model.UserTests.Count > 0)
        {
            var totalPageResult = 0;
            int posResult = -1;
            var userTests = Model.UserTests.Where(ut => ut.TestId != null && ut.FinishTime != null && ut.UserId == int
                .Parse(HttpContext.Current.User.Identity.Name)).OrderByDescending(ut => ut.UserTestId);
            if(userTests.Count()==0)
            {
                <span>You haven't taken this test yet!!</span>
            }
            foreach (var item in userTests)
            {
                posResult++;
                if (posResult % 10 == 0)
                {
                    totalPageResult++;
                }
                var pageClassResult = "ut" + totalPageResult;

        <table class="table table-bordered @pageClassResult userTest" style="table-layout: fixed">
            <tr style="background-color: #D9EDF7">
                @*                            <td style="width: 50px">
                                <span>No </span>
                            </td>*@
@*                <td>
                    <span>Taken By </span>
                </td>*@
                <td>
                    <span>Start Time </span>
                </td>
                <td>
                    <span>Finish Time </span>
                </td>
                <td>
                    <span>Duration </span>
                </td>
                <td>
                    <span>Total Marks</span>
                </td>
            </tr>
            <tr>
@*                <td>
                    <span>
                        @Html.ActionLink(item.User.FullName, "Index", "Account", new { userId = item.UserId }, null)
                    </span>
                </td>*@
                <td>
                    <span>
                        @item.StartTime
                    </span>
                </td>
                <td>
                    <span>
                        @item.FinishTime
                    </span>
                </td>
                <td>
                    <span>
                        @{
                var duration = (item.FinishTime.Value - item.StartTime).Minutes;
                            <span>@duration minutes</span>
                        }
                    </span>
                </td>
                <td>
                    <span>
                        @{
                            var mark = Math.Round(item.Mark.Value/100, 2).ToString() + "/" + @item.Test.TestQuestions.Count.ToString()
                                + " (" + Math.Round(item.Mark.Value / 10 / item.Test.TestQuestions.Count, 2) + "/10)";
                        }
                        @Html.ActionLink(@mark, "TestResult", new {id = item.UserTestId}, new {target = "_blank"})
@*                        @(Math.Round(item.Mark.Value / 100, 2))/@item.Test.TestQuestions.Count
                        (@(Math.Round(item.Mark.Value / 10 / item.Test.TestQuestions.Count, 2))/10)*@
                    </span>
                </td>
            </tr>
        </table>
            }
        <table width="100%">
            <tr>
                <td>
                    <div>
                        <span>Page: </span><span id="pageResult">1</span><span>/@ViewBag.totalPageResult</span>
                    </div>
                    <div id="page-selectionResult">
                    </div>
                    <script>
                            $('#page-selectionResult').bootpag({
                                total: @totalPageResult,
                            }).on("page", function (event, num) {
                                goToPageResult(num, @totalPageResult);
                                $("#pageResult").html(num);
                            });
                    </script>
                </td>
            </tr>
        </table>
        }
        else
        {
        <div style="padding-left: 5px">
            Empty.</div>
        }
    }
</div>
